package top.lixunda.ecommerce.server.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import top.lixunda.common.utils.ObjectValidators;
import top.lixunda.ecommerce.server.business.BaseServiceImpl;
import top.lixunda.ecommerce.server.business.api.entity.cache.CacheLoginUser;
import top.lixunda.ecommerce.server.user.api.entity.db.UserPermissionDO;
import top.lixunda.ecommerce.server.user.mapper.UserPermissionMapper;
import top.lixunda.ecommerce.server.user.service.IUserPermissionService;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户权限视图，查看用户权限，已经处理了必要的权限条件判断 服务实现类
 * </p>
 *
 * @author Lixunda
 * @since 2020-04-24
 */
@Service
public class UserPermissionServiceImpl
        extends BaseServiceImpl<UserPermissionMapper, UserPermissionDO>
        implements IUserPermissionService {

    /**
     * 根据账号查询用户权限
     *
     * @param account 用户账户
     * @return 权限列表
     */
    @Override
    public List<UserPermissionDO> queryPermissionsByAccount(Integer account) {
        return this.queryPermissionsByAccountAndType(account, null);
    }

    /**
     * 根据账号查询用户权限
     *
     * @param account  用户账户
     * @param userType 用户类型
     * @return 权限列表
     */
    @Override
    public List<UserPermissionDO> queryPermissionsByAccountAndType(Integer account, String userType) {
        if (ObjectValidators.isEmpty(account)) {
            throw getAppException("查询权限失败！用户编号不能为空！");
        }
        LambdaQueryWrapper<UserPermissionDO> wrapper = new LambdaQueryWrapper<UserPermissionDO>()
                .eq(UserPermissionDO::getAccount, account);
        if (ObjectValidators.isNotEmpty(userType)) {
            wrapper.eq(UserPermissionDO::getUserType, userType);
        }
        return this.list(wrapper);
    }

    /**
     * 查询当前登陆账户的权限
     *
     * @return 权限列表
     */
    @Override
    public List<UserPermissionDO> queryCurrentPermissions() {
        CacheLoginUser cacheLoginUser = currentLoginUserCacheNotNull();
        return this.queryPermissionsByAccountAndType(cacheLoginUser.getAccount(), cacheLoginUser.getType());
    }

    /**
     * 根据多个用户账户查询多个账户的权限
     *
     * @param accounts 账户列表
     * @return 多个账户的权限列表，键为用户编号，值为权限列表
     */
    @Override
    public Map<Integer, List<UserPermissionDO>> queryUsersPermissions(List<Integer> accounts) {
        if (ObjectValidators.isEmpty(accounts)) {
            throw getAppException("查询权限失败！用户编号列表不能为空！");
        }
        List<UserPermissionDO> list = this.list(new LambdaQueryWrapper<UserPermissionDO>()
                .in(UserPermissionDO::getAccount, accounts));
        Map<Integer, List<UserPermissionDO>> map = list.stream()
                .collect(Collectors.toMap(UserPermissionDO::getAccount, item -> {
                    List<UserPermissionDO> tempList = new ArrayList<>(2);
                    tempList.add(item);
                    return tempList;
                }, (item1, item2) -> {
                    item1.addAll(item2);
                    return item1;
                }));
        accounts.forEach(item -> {
            if (ObjectValidators.isEmpty(map.get(item))) {
                map.put(item, Collections.emptyList());
            }
        });
        return map;
    }
}
